package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.TaskDao;
import com.wanlian.entity.Device;
import com.wanlian.entity.Task;
import com.wanlian.entity.User;
import com.wanlian.entity.UserTask;
import com.wanlian.model.ResultModel;
import com.wanlian.sql.SqlUtil;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TaskDaoImpl extends GenericDaoImpl<Task> implements TaskDao {

    public TaskDaoImpl() {
        super(Task.class);
    }


    /**
     * 查询任务列表
     *
     * @param pageCondition
     */
    @Override
    public ResultModel queryTaskByCondition(PageCondition pageCondition) {
        String sql="select t.id,t.name,t.ranktype,t.rank,t.isenble,t.modifytime,u.username   from tb_task t left join tb_user u on t.userid=u.id where 1=1 ";
        StringBuilder sqlWhere=new StringBuilder();
        String[] column = {"id","name", "rankType", "rank", "isEnble", "modifyTime", "username"};
        ArrayList<Object> param = new ArrayList<Object>();
        if (pageCondition.isEmpty("username")) {
            String username = (String) pageCondition.getByKey("username");
            sqlWhere.append(" and u.username like ?");
            param.add("%" + username + "%");
        }
        if (pageCondition.isEmpty("rankType")) {
            int rankType = Integer.parseInt((String) pageCondition.getByKey("rankType"));
            sqlWhere.append(" and t.rankType=?");
            param.add(rankType);
        }
        if (pageCondition.isEmpty("rank")) {
            int rank = Integer.parseInt((String) pageCondition.getByKey("rank"));
            sqlWhere.append(" and t.rank=?");
            param.add(rank);
        }
        if (pageCondition.isEmpty("name")) {
            String name = (String) pageCondition.getByKey("name");
            sqlWhere.append(" and t.name=?");
            param.add(name);
        }
        if (pageCondition.isEmpty("startTime")&&pageCondition.isEmpty("endTime")) {
            String startTime = (String) pageCondition.getByKey("startTime");
            String endTime = (String) pageCondition.getByKey("endTime");
            sqlWhere.append(" and t.modifytime between date(?) and date(?) ");
            param.add(startTime);
            param.add(endTime);
        }
        List<Map<String, Object>> lists = this.queryForListObjectByPage(sql+sqlWhere + "", param.toArray(), pageCondition.getPage(), column);
        int count =this.queryCountByParam("select count(1) from tb_task t,tb_user u where t.userid=u.id "+sqlWhere,param.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;

    }

    /**
     * 查询用户任务列表
     *
     * @param pageCondition
     */
    @Override
    public ResultModel queryUserTaskByCondition(PageCondition pageCondition) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();

        if (pageCondition.isEmpty("userId")) {
            int userId = (int) pageCondition.getByKey("userId");
            sqlWhere.append(" and userId =? ");
            param.add(userId);
        }

        if (pageCondition.isEmpty("taskId")) {
            int taskId = (int) pageCondition.getByKey("taskId");
            sqlWhere.append(" and taskId =? ");
            param.add(taskId);
        }
        List<Task> lists = new ArrayList<Task>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        lists = this.queryForListByPage(hql, param.toArray(), pageCondition.getPage());
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }

    /**
     * 添加发布积分任务
     */
    @Override
    public List<Task> queryTasksByName(String name) {
        List<Task> lists = new ArrayList<Task>();
        String like = SqlUtil.getLikeCondition(name);
        String hql = String.format("from %s where name like ?", entityClassName());
        lists = this.queryForList(hql, new Object[]{like});
        return lists;
    }

    @Override
    public List<Task> queryTasksByParams(PageCondition pageCondition) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();
        if(pageCondition.isEmpty("name")){
            String name=(String)pageCondition.getByKey("name");
            sqlWhere.append(" and name=? ");
            param.add(name);
        }

        if(pageCondition.isEmpty("rankType")){
            int rankType=Integer.parseInt((String)pageCondition.getByKey("rankType"));
            sqlWhere.append(" and rankType=? ");
            param.add(rankType);
        }

        List<Task> lists = new ArrayList<Task>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        lists = this.queryForList(hql, param.toArray());
        return lists;
    }


    @Override
    public Task queryTaskByName(String name) {
        String hql = String.format("from %s where name=? and isenble=true ", entityClassName());
        Task task = this.queryForObject(hql, new Object[]{name});
        return task;
    }
}
